package com.yxdj.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yxdj.common.core.web.domain.AjaxResult;
import com.yxdj.common.security.utils.SecurityUtils;
import com.yxdj.order.api.domain.OrderInfo;
import com.yxdj.order.domain.dto.AppointmentDto;
import com.yxdj.order.domain.dto.CancelOrderDto;
import com.yxdj.order.domain.dto.OrderInfoDto;
import com.yxdj.order.domain.vo.AppointmentSubmitVo;
import com.yxdj.order.service.OrderInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 韩总
 */
@Tag(name = "用户订单管理")
@RestController
@RequestMapping("/appUser")
public class AppUserOrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @Operation(summary = "用户去立即预约页面")
    @GetMapping("/toAppointmentPage")
    public AjaxResult toAppointmentPage(AppointmentDto appointmentDto){
        //去预约页面
        AppointmentSubmitVo appointmentSubmitVo = orderInfoService.toAppointmentPage(appointmentDto);
        return AjaxResult.success(appointmentSubmitVo);
    }

    @Operation(summary = "用户提交预约订单")
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderInfoDto orderInfoDto){
        //提交预约订单
        Long orderId = orderInfoService.saveOrder(orderInfoDto);
        return AjaxResult.success(orderId);
    }

    @Operation(summary = "用户获取我的订单")
    @GetMapping("/getUserOrder/{status}")
    public AjaxResult getUserOrder(@PathVariable("status") Integer status){
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //获取我的订单
        List<OrderInfo> orderInfoList = orderInfoService.list(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getUserId, 1).eq(status != -1,OrderInfo::getOrderStatus, status));
        return AjaxResult.success(orderInfoList);
    }

    @Operation(summary = "用户取消订单")
    @PostMapping("/cancelOrder")
    public AjaxResult cancelOrder(@RequestBody CancelOrderDto cancelOrderDto){
        //取消订单
        orderInfoService.update(new LambdaUpdateWrapper<OrderInfo>().set(OrderInfo::getOrderStatus, 5).set(OrderInfo::getCancelReason, cancelOrderDto.getCancelReason()).set(OrderInfo::getUpdateTime,new Date()).eq(OrderInfo::getId, cancelOrderDto.getOrderId()));
        return AjaxResult.success();
    }
}
